package com.zy.qudadid.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rey.material.app.Dialog;
import com.rey.material.widget.Button;
import com.yancy.imageselector.ImageConfig;
import com.yancy.imageselector.ImageSelectorActivity;
import com.zy.lcpassenger.R;
import com.zy.qudadid.presenter.RegisterCUploadPresenter;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.ui.view.RegisterCUploadView;
import com.zy.qudadid.ui.widget.RoundCornerImageView;
import com.zy.qudadid.utils.ActivityUtil;
import com.zy.qudadid.utils.GlideLoader;
import com.zy.qudadid.utils.ImageSelectorUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by ninos on 2016/7/30.
 */
public class RegisterCUploadActivity extends ToolBarActivity<RegisterCUploadPresenter> implements RegisterCUploadView {

    private String phoneNumber = "";
    private String pwd = "";
    private String path = "";
    private String name = "";
    private String carnumber = "";
    private String zhunjia = "";
    String shenfenzheng_off_str = "";
    String shenfenzheng_on_str = "";
    String jiashizheng_off_str = "";
    String jiashizheng_on_str = "";
    String qita_off_str = "";
    String qita_on_str = "";
    private Dialog dialog;
    private View view;

    @Bind(R.id.l_login)Button l_login;
    @Bind(R.id.shenfenzheng_off)LinearLayout shenfenzheng_off;
    @Bind(R.id.shenfenzheng_on)LinearLayout shenfenzheng_on;
    @Bind(R.id.jiashizheng_off)LinearLayout jiashizheng_off;
    @Bind(R.id.jiashizheng_on)LinearLayout jiashizheng_on;
    @Bind(R.id.qita_off)LinearLayout qita_off;
    @Bind(R.id.qita_on)LinearLayout qita_on;
    @Bind(R.id.shenfenzheng_off_iv)RoundCornerImageView shenfenzheng_off_iv;
    @Bind(R.id.shenfenzheng_on_iv)RoundCornerImageView shenfenzheng_on_iv;
    @Bind(R.id.jiashizheng_off_iv)RoundCornerImageView jiashizheng_off_iv;
    @Bind(R.id.jiashizheng_on_iv)RoundCornerImageView jiashizheng_on_iv;
    @Bind(R.id.qita_off_iv)RoundCornerImageView qita_off_iv;
    @Bind(R.id.qita_on_iv)RoundCornerImageView qita_on_iv;
    @Bind(R.id.shenfenzheng_off_tv)TextView shenfenzheng_off_tv;
    @Bind(R.id.shenfenzheng_on_tv)TextView shenfenzheng_on_tv;
    @Bind(R.id.jiashizheng_off_tv)TextView jiashizheng_off_tv;
    @Bind(R.id.jiashizheng_on_tv)TextView jiashizheng_on_tv;
    @Bind(R.id.qita_off_tv)TextView qita_off_tv;
    @Bind(R.id.qita_on_tv)TextView qita_on_tv;

    @OnClick(R.id.shenfenzheng_off)
    public void shenfenzheng_off(){
        ImageConfig imageConfig
                = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
                .titleBgColor(getResources().getColor(R.color.colorPrimary))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // (截图默认配置：关闭    比例 1：1    输出分辨率  500*500)
                .crop(1,1,2000,2000)
                // 开启单选   （默认为多选）
                .singleSelect()
                // 开启拍照功能 （默认关闭）
                .showCamera()
                // 拍照后存放的图片路径（默认 /temp/picture） （会自动创建）
                .filePath("/ImageSelector/Pictures")
                .build();
        ImageSelectorUtil.open(this, imageConfig, 2048);   // 开启图片选择器
    }

    @OnClick(R.id.shenfenzheng_on)
    public void shenfenzheng_on(){
        ImageConfig imageConfig
                = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
                .titleBgColor(getResources().getColor(R.color.colorPrimary))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // (截图默认配置：关闭    比例 1：1    输出分辨率  500*500)
                .crop(1,1,2000,2000)
                // 开启单选   （默认为多选）
                .singleSelect()
                // 开启拍照功能 （默认关闭）
                .showCamera()
                // 拍照后存放的图片路径（默认 /temp/picture） （会自动创建）
                .filePath("/ImageSelector/Pictures")
                .build();


        ImageSelectorUtil.open(this, imageConfig, 2049);   // 开启图片选择器
    }

    @OnClick(R.id.jiashizheng_off)
    public void jiashizheng_off(){
        ImageConfig imageConfig
                = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
                .titleBgColor(getResources().getColor(R.color.colorPrimary))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // (截图默认配置：关闭    比例 1：1    输出分辨率  500*500)
                .crop(1,1,2000,2000)
                // 开启单选   （默认为多选）
                .singleSelect()
                // 开启拍照功能 （默认关闭）
                .showCamera()
                // 拍照后存放的图片路径（默认 /temp/picture） （会自动创建）
                .filePath("/ImageSelector/Pictures")
                .build();


        ImageSelectorUtil.open(this, imageConfig, 2050);   // 开启图片选择器
    }

    @OnClick(R.id.jiashizheng_on)
    public void jiashizheng_on(){
        ImageConfig imageConfig
                = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
                .titleBgColor(getResources().getColor(R.color.colorPrimary))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // (截图默认配置：关闭    比例 1：1    输出分辨率  500*500)
                .crop(1,1,2000,2000)
                // 开启单选   （默认为多选）
                .singleSelect()
                // 开启拍照功能 （默认关闭）
                .showCamera()
                // 拍照后存放的图片路径（默认 /temp/picture） （会自动创建）
                .filePath("/ImageSelector/Pictures")
                .build();


        ImageSelectorUtil.open(this, imageConfig, 2051);   // 开启图片选择器
    }

    @OnClick(R.id.qita_off)
    public void qita_off(){
        ImageConfig imageConfig
                = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
                .titleBgColor(getResources().getColor(R.color.colorPrimary))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // (截图默认配置：关闭    比例 1：1    输出分辨率  500*500)
                .crop(1,1,2000,2000)
                // 开启单选   （默认为多选）
                .singleSelect()
                // 开启拍照功能 （默认关闭）
                .showCamera()
                // 拍照后存放的图片路径（默认 /temp/picture） （会自动创建）
                .filePath("/ImageSelector/Pictures")
                .build();


        ImageSelectorUtil.open(this, imageConfig, 2052);   // 开启图片选择器
    }

    @OnClick(R.id.qita_on)
    public void qita_on(){
        ImageConfig imageConfig
                = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
                .titleBgColor(getResources().getColor(R.color.colorPrimary))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // (截图默认配置：关闭    比例 1：1    输出分辨率  500*500)
                .crop(1,1,2000,2000)
                // 开启单选   （默认为多选）
                .singleSelect()
                // 开启拍照功能 （默认关闭）
                .showCamera()
                // 拍照后存放的图片路径（默认 /temp/picture） （会自动创建）
                .filePath("/ImageSelector/Pictures")
                .build();


        ImageSelectorUtil.open(this, imageConfig, 2053);   // 开启图片选择器
    }

    @OnClick(R.id.l_login)
    public void l_login(){
        if(shenfenzheng_off_str.trim().equals("")||
        shenfenzheng_on_str.trim().equals("")||
        jiashizheng_off_str.trim().equals("")||
        jiashizheng_on_str.trim().equals("")||
        qita_off_str.trim().equals("")||
        qita_on_str.trim().equals("")){
            toast("请确保证件全部上传");
            return;
        }
        presenter.DriverImageHeader(phoneNumber,path,"7");
    }

    @Override
    protected CharSequence provideTitle() {
        return "完成注册";
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_register_c_upload;
    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
        phoneNumber = getIntent().getBundleExtra("data").getString("phoneNumber");
        pwd = getIntent().getBundleExtra("data").getString("pwd");
        path = getIntent().getBundleExtra("data").getString("path");
        name = getIntent().getBundleExtra("data").getString("name");
        carnumber = getIntent().getBundleExtra("data").getString("carnumber");
        zhunjia = getIntent().getBundleExtra("data").getString("zhunjia");
        view = getLayoutInflater().inflate(R.layout.dialog_publish_info, null);
    }

    private void showDialogExitLogin(){
        if(dialog == null)
            dialog = new Dialog(this).backgroundColor(Color.parseColor("#ffffff")).contentView(view);
        dialog.setOnDismissListener((v) -> finishAllActivity() );
        dialog.show();
    }

    public void finishAllActivity(){
        ActivityUtil.finishActivitys();
        finish();
    }

    @Override
    public void initListeners() {

    }

    @Override
    public RegisterCUploadPresenter createPresenter() {
        return new RegisterCUploadPresenter();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 2048 && resultCode == RESULT_OK && data != null) {
            // Get Image Path List
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(pathList.size()-1));
            shenfenzheng_off_str = pathList.get(pathList.size()-1);

            Glide.with(this)
                    .load(shenfenzheng_off_str)
                    .centerCrop()
                    .crossFade()
                    .placeholder(R.drawable.userimg)
                    .into(shenfenzheng_off_iv);

            shenfenzheng_off_iv.setVisibility(View.VISIBLE);
            shenfenzheng_off_tv.setVisibility(View.GONE);
            presenter.DriverImage(phoneNumber,shenfenzheng_off_str,"3");
        }
        if (requestCode == 2049 && resultCode == RESULT_OK && data != null) {
            // Get Image Path List
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(pathList.size()-1));
            shenfenzheng_on_str = pathList.get(pathList.size()-1);
            Glide.with(this)
                    .load(shenfenzheng_on_str)
                    .centerCrop()
                    .crossFade()
                    .placeholder(R.drawable.userimg)
                    .into(shenfenzheng_on_iv);

            shenfenzheng_on_iv.setVisibility(View.VISIBLE);
            shenfenzheng_on_tv.setVisibility(View.GONE);
            presenter.DriverImage(phoneNumber,shenfenzheng_on_str,"5");
        }
        if (requestCode == 2050 && resultCode == RESULT_OK && data != null) {
            // Get Image Path List
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(pathList.size()-1));
            jiashizheng_off_str = pathList.get(pathList.size()-1);
            Glide.with(this)
                    .load(jiashizheng_off_str)
                    .centerCrop()
                    .crossFade()
                    .placeholder(R.drawable.userimg)
                    .into(jiashizheng_off_iv);

            jiashizheng_off_iv.setVisibility(View.VISIBLE);
            jiashizheng_off_tv.setVisibility(View.GONE);
            presenter.DriverImage(phoneNumber,jiashizheng_off_str,"9");
        }
        if (requestCode == 2051 && resultCode == RESULT_OK && data != null) {
            // Get Image Path List
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(pathList.size()-1));
            jiashizheng_on_str = pathList.get(pathList.size()-1);
            Glide.with(this)
                    .load(jiashizheng_on_str)
                    .centerCrop()
                    .crossFade()
                    .placeholder(R.drawable.userimg)
                    .into(jiashizheng_on_iv);

            jiashizheng_on_iv.setVisibility(View.VISIBLE);
            jiashizheng_on_tv.setVisibility(View.GONE);
            presenter.DriverImage(phoneNumber,jiashizheng_on_str,"12");

        }
        if (requestCode == 2052 && resultCode == RESULT_OK && data != null) {
            // Get Image Path List
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(pathList.size()-1));
            qita_off_str = pathList.get(pathList.size()-1);
            Glide.with(this)
                    .load(qita_off_str)
                    .centerCrop()
                    .crossFade()
                    .placeholder(R.drawable.userimg)
                    .into(qita_off_iv);

            qita_off_iv.setVisibility(View.VISIBLE);
            qita_off_tv.setVisibility(View.GONE);
            presenter.DriverImage(phoneNumber,qita_off_str,"10");
        }
        if (requestCode == 2053 && resultCode == RESULT_OK && data != null) {
            // Get Image Path List
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(pathList.size()-1));
            qita_on_str = pathList.get(pathList.size()-1);
            Glide.with(this)
                    .load(qita_on_str)
                    .centerCrop()
                    .crossFade()
                    .placeholder(R.drawable.userimg)
                    .into(qita_on_iv);

            qita_on_iv.setVisibility(View.VISIBLE);
            qita_on_tv.setVisibility(View.GONE);
            presenter.DriverImage(phoneNumber,qita_on_str,"11");
        }

    }

    @Override
    public void errorRegister(String msg) {
        toast("请确保在网络畅通的条件下重新点击完成按钮。");
    }

    @Override
    public void successRegister() {
        showDialogExitLogin();
    }

    @Override
    public void errorDriverImage() {
        toast("上传证件失败,请重新上传");
    }
}
